define(['jquery','api','user','tmpl','util','swiper','echarts','pages/index/goodsList','pages/index/model','pages/index/order','pages/index/refresh','pages/index/charts','pages/index/groupNav','text!pages/index/index.tpl','text!pages/index/goodsCt.html','text!pages/index/charts.html','pages/index/notice'],function($,api,user,tmpl,util){
	var event=function(){
	    	mySwiper.init();
		    groupNav.init();
	    	order.event();
	    	refresh.init();
	    	charts.init();
	    },
	    order=require('pages/index/order'),
	    mySwiper=require('pages/index/goodsList'),
	    refresh=require('pages/index/refresh'),
	    groupNav=require('pages/index/groupNav'),
	    charts=require('pages/index/charts'),
	    model=require('pages/index/model'),
	    notice=require('pages/index/notice');
	var controller = {
		init:function () {
	       var view=$('#index'),
	           that=this,
	           data;
	       if(api.showQrcode())return;
	       if(!user.checkLogin())return;
	       api.initFooter(0);
	   	   require('util').setTitle('青西微交易');
	   	   notice.init();
	       if(view.length&&model.code){
		   	   view.show();
		   	   that.loadUserInfo();
		   	   event();
		   }else{
		       require(['text!pages/index/index.tpl','swiper'],function(text){
		       		var headimgurl = $.cookie('headimgurl') ? $.cookie('headimgurl') : user.data.headimgurl;
		           var userFmt={headimgurl:headimgurl,nickname:$.cookie('nickname')},
		               str=tmpl(text,{user:userFmt});
		           $('#Loading').hide();
		           $('#pages').append(str);
		           that.loadUserInfo();
		           if(model.code==null){
			   	   		data=api.getGoods();
			   	   		if(!data||(data.errorcode&&data.errorcode!=0)||data.retcode!='0'){
			   	   		    $('#index').remove();
			   	   		    return;
			   	   		}else{
			   	   			model.addGoods(data);
			   	   		}
		           }
	   	   		   require(['text!pages/index/goodsCt.html','text!pages/index/charts.html'],function(text,chartTpl){
		   	   			str=tmpl(text,{groups:model.groups_arr,goods_arr:model.goods_obj[model.groupid]});	   	   			
		   	   			$('#goods_ct').html(str);		   	   			
		   	   			$('#index').append(chartTpl);
		   	   			event();
	   	   		   });
		           
		       })
		   }
	    },
	    loadUserInfo:function(){
	    	var pl,
	    	    cls,
	    	    cls2;
	    	api.userInfo(function(rs){
	    	    if(rs.retcode==='0'){
	    	        $('#balanceCt').html(util.priceFormat(2,+rs.userfund.balance));
	    	        $('#couponNuberCt').html(rs.couponsize);
	    	        $('#nickname').html(rs.userfund.username ? rs.userfund.username : '--');
	    	        cls='cl-'+(rs.userfund.pl>=0?'up':'down');
	    	        cls2='cl-'+(rs.userfund.pl>=0?'down':'up');
	    	        pl=util.priceFormat(2,+rs.userfund.pl);
	    	        $('#plCt').html(pl).removeClass(cls2).addClass(cls);
	    	    }
	    	})
	    },
    	
        onRouteChange:function(){
			$('#index').hide();
			refresh.clearTimer();
			charts.clearTimer();
		}
	}
    return controller;     
})